﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;

public static class StringUtility
{
    static readonly string regexIsNumeric = @"^[+-]?\d*[.]?\d*$";
    public static readonly string StrEmpty = ""; 
    static readonly char charSplit = '#';
    static readonly char charSemicolon = ';';

    /// <summary>
    /// 判断是否是数字
    /// </summary>
    /// <param name="value">字符串</param>
    /// <returns></returns>
    public static bool IsNumeric(string value)
    {
        return Regex.IsMatch(value, regexIsNumeric);
    }

    public static int[][] StringToIntArrayArray(string str)
    {
        if (str.IsEmpty() == true
        || str.Equals(StrEmpty) == true
        )
        {
            return null;
        }

        string[] strArray = str.Split(charSemicolon);

        int[][] retArray = new int[strArray.Length][];

        for (int i = 0; i < strArray.Length; i++)
        {
            retArray[i] = StringToIntArray(strArray[i]);
        }

        return retArray;
    }

    public static int[] StringToIntArray(string str)
    {
        if (str.IsEmpty() == true
        || str.Equals(StrEmpty) == true
        )
        {
            return null;
        }
#if UNITY_EDITOR
        try
        {
#endif
            string[] strArray = str.Split(charSplit);

            int[] retArray = new int[strArray.Length];

            for (int i = 0; i < strArray.Length; i++)
            {
                string value = strArray[i].Trim();

                if (IsNumeric(value) == true)
                {
                    retArray[i] = Convert.ToInt32(value);
                }
                else
                {
                    retArray[i] = 0;
                    Debug.LogError("StringToIntArray Error!!!!! Value is :" + str);
                }
            }

            return retArray;
#if UNITY_EDITOR
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }

        return null;
#endif
    }

    public static string MakeStrKey(int ID, int ID1)
    {

        return ID + "|" + ID1;
    }

    public static string MakeTextKey(string ID, string ID1)
    {
        return ID + "_" + ID1;
    }

    public static string MakeStrKey(int ID, int ID1, int ID2)
    {
        return ID + "|" + ID1 + "|" + ID2;
    }

    public static bool IsEmpty<T>(this IEnumerable<T> enumerable)
    {
        return !enumerable.Any();
    }

}
